<?php
/**
  * 系統名稱: Platform 系統
  * 檔案說明: 專案管理-修改
  * $Author$
  * $Id$
  *
  */

if (!defined("AdminProgram_lib_include")){
	define("AdminProgram_lib_include", 1);
	include_once(__Modules_Path."/LogRecord.lib.php");

/**
 * 取得所有父層目錄
 * @return	false -> 錯誤，array -> 成功, 
 */
	function getAllProgramParentFolder(){
		global $_Conn, $$_Conn;

		$strSQL = "select ID, `ProgramName_"._LangCode."` as Name from `bet_Program` where ProgramURL = ''";

		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Program Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return array();
		while($objPg = PMA_mysql_fetch_object($RS)){
			$arrPg[$objPg -> ID] = $objPg -> Name;
		}
		return $arrPg;
	}


/**
 * 取得所有程式連結
 * @return	false -> 錯誤，array -> 成功, 
 */
	function getAllProgramLink(){
		global $_Conn, $$_Conn;

		$strSQL = "select ID, PgCode, PowerMemo, `ProgramName_"._LangCode."` as Name from `bet_Program` where ProgramURL != ''";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Program Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return array();
		while($objPg = PMA_mysql_fetch_object($RS)){
			$arrPg[$objPg -> ID]["Name"] = $objPg -> Name;
			$arrPg[$objPg -> ID]["PowerMemo"] = $objPg -> PowerMemo;
			$arrPg[$objPg -> ID]["PgCode"] = $objPg -> PgCode;
		}
		return $arrPg;
	}


/**
 * 取得程式連結資料
 * @param	intID 序號
 * @return	false -> 錯誤，object -> 成功, 
 */
	function getObjProgramData($intID){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_Program` where ID = %d";
		$strSQL = sprintf($strSQL, $intID);

		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Program Data Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($$_Conn).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		if(PMA_mysql_num_rows($RS) == 0)return object;
		return PMA_mysql_fetch_object($RS);
	}

/**
 * 刪除一筆程式連結資料
 * @param	intID 上層序號
 * @return	false -> 錯誤，int -> 成功, 
 */
	function rmProgram($intID){
		global $_ConnMaster;

		if(!DeleteData_Log($_SESSION["Admin_Info"] -> ID, "bet_Program", $intID))return false;

		$strSQL = "delete from `bet_Program` where ID = %d";
		$strSQL = sprintf($strSQL, $intID);

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Remove Program Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		return true;
	}

/**
 * 新增一筆程式連結資料
 * @param	intPID 上層序號
 * @param	arrPName 語言名陣列
 * @param	strPUrl 程式連結
 * @param	intPSNo 優先加權
 * @param	strPgCode 程式識別代碼
 * @param	strPgMemo 程式別名
 * @param	strPowerMemo 程式權限設定
 * @return	false -> 錯誤，int -> 成功, 
 */
	function setAddProgram($intPID, $arrPName, $strPUrl, $intPSNo, $strPgCode, $strPgMemo, $strPowerMemo){
		global $_ConnMaster;

		$strLog = "";
		$arrF = array();
		$arrV = array();
		foreach($arrPName as $intLang => $strPName){
			$arrF[] = "`ProgramName_".$intLang."`";
			$arrV[] = "'".$strPName."'";
			$strLog .= "ProgramName_".$intLang." => ".$strPName."\n";
		}

		$strSQL = "insert into `bet_Program`(`ParentID`, `ProgramURL`, `SerialNO`, `PgCode`, `PgMemo`, `PowerMemo`, ".join(", ", $arrF).") ";
		$strSQL .= "values(".$intPID.", '".$strPUrl."', ".$intPSNo.", '".$strPgCode."', '".$strPgMemo."', '".$strPowerMemo."', ".join(", ", $arrV).")";

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Add Program Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}
		$intNewID = mysql_insert_id($_ConnMaster);

		$strLog = "ID => ".$intNewID."\n".$strLog;
		$strLog .= "PID => ".$intPID."\n";
		$strLog .= "PUrl => ".$strPUrl."\n";
		$strLog .= "PSNo => ".$intPSNo."\n";
		$strLog .= "PgCode".$strPgCode."\n";
		$strLog .= "PgMemo => ".$strPgMemo."\n";
		$strLog .= "PowerMemo => ".$strPowerMemo."\n";

		AddData_Log($_SESSION["Admin_Info"] -> ID, "新增程式管理完成", $strLog);

		//if(!setAddLangName($intNewID, "wg_Program", $arrPName))return false;

		return $intNewID;
	}


/**
 * 修改一筆程式連結資料
 * @param	intID 序號
 * @param	intPID 上層序號
 * @param	arrPName 語言名陣列
 * @param	strPUrl 程式連結
 * @param	intPSNo 優先加權
 * @param	strPgCode 程式識別代碼
 * @param	strPgMemo 程式別名
 * @param	strPowerMemo 程式權限設定
 * @return	false -> 錯誤，true -> 成功, 
 */
	function setProgram($intID, $intPID, $arrPName, $strPUrl, $intPSNo, $strPgCode, $strPgMemo, $strPowerMemo){
		global $_ConnMaster;

		$strLog = "";
		$strSQL = "update `bet_Program` set ";
		foreach($arrPName as $intLang => $strPName){
			$strSQL .= "`ProgramName_".$intLang."` = '".$strPName."', ";
			$strLog .= "ProgramName_".$intLang." => ".$strPName."\n";
		}
		$strSQL .= "`ParentID` = ".$intPID.", ".
				   "`ProgramURL` = '".$strPUrl."', ".
				   "`SerialNO` = ".$intPSNo.", ".
				   "`PgCode` = '".$strPgCode."', ".
				   "`PgMemo` = '".$strPgMemo."', ".
				   "`PowerMemo` = '".$strPowerMemo."' ".
				   "where ID = ".$intID;

		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Edit Program Error!!";
			$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
			return false;
		}

		$strLog = "ID => ".$intID."\n".$strLog;
		$strLog .= "PID => ".$intPID."\n";
		$strLog .= "PUrl => ".$strPUrl."\n";
		$strLog .= "PSNo => ".$intPSNo."\n";
		$strLog .= "PgCode".$strPgCode."\n";
		$strLog .= "PgMemo => ".$strPgMemo."\n";
		$strLog .= "PowerMemo => ".$strPowerMemo."\n";

		AddData_Log($_SESSION["Admin_Info"] -> ID, "修改程式管理完成", $strLog);
		return true;
	}


	/**
	 * 根據 program id 找出其上層所有 id
	 * @param	strPgID 程式連結序號，ex: 2,4,5,6,.......,n
	 * @return	成功回傳 字串 ，ex: 2,4,5,6,.......,n
	 */
	function getAllProgramID($strPgID){
		global $_Conn, $$_Conn;
		if(strlen($strPgID) == 0)return "";

		$strSQL = "select ParentID from `bet_Program` where ID in (".$strPgID.") Group by ParentID Order by SerialNO desc, ID";
		//print $strSQL."\n";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Get Program Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		if(PMA_mysql_num_rows($RS) == 0)return $strPgID;

		$arrPID = array();
		while($objPg = PMA_mysql_fetch_object($RS)){
			$arrPID[] = $objPg -> ParentID;
		}
		@mysql_free_result($RS);
		$strTmp = getAllProgramID(implode(",", $arrPID));
		if($strTmp != "")$strPgID .= ",".$strTmp;
		return $strPgID;
	}


/**
 * 產生管理介面 NavBar
 * 輸出
 * NavBar?x_?y.php：管理介面 Navbar 參數檔
 * NavBarNo?x_?y.php：管理介面 Navbar 參數檔，不含即時注單功能
 * ?x：群組代號，2 -> 代理商，3 -> 總代理......依 wg_MemGroup 裡頭定義
 * ?y：語言版本，1 -> 繁體， 2 -> 英文， 3 -> 簡體
 * @return	成功回傳 true，否 false
 */
	function getAdminMenu(){
		global $_Conn, $$_Conn;
		$arrLan = explode(",", __LanguageAssembly);

		//找出各群組的 program id
		$strSQL = "select ID, ProgramID from `bet_MemberGroup` where ID != 1";
		if(!$rsUG = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Make Menu File Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		while($objUG = PMA_mysql_fetch_object($rsUG)){
			//依各群組 program id 找出其上層所有 program id
			if(empty($objUG -> ProgramID))$objUG -> ProgramID = "0";

			$strPID = getAllProgramID($objUG -> ProgramID);
			if($strPID === false)return false;
			if($strPID == "")continue;

			//找出所有最上層 目錄
			$strSQL = "select * from `bet_Program` where ParentID = 0 and ID in (".$strPID.") order by SerialNO desc, ID";
			if(!$rsPg = @PMA_mysql_query($strSQL, $$_Conn)){
				$GLOBALS["errormessage"] = "Get Program Data Error!!";
				$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
				return false;
			}
			$intPgRows = PMA_mysql_num_rows($rsPg);

			foreach($arrLan as $intLang => $strLCode){
				if($intLang == 0)continue;

				$strCon = "";
				$strConNoBetList = "";
				$strSubMenu = "";
				$kk = 1;
				$strSpace = ($strLCode == "en")?"&nbsp;&nbsp;":"";

				for ($i = 0; $i < $intPgRows; $i++) {
					if (strlen(PMA_mysql_result($rsPg, $i, "ProgramURL")) > 0) {
						$strTmpCon =  "<a href=\"".PMA_mysql_result($rsPg,$i,"ProgramURL")."\" class=\"top_admin\" target=\"bottom\">".PMA_mysql_result($rsPg, $i, "ProgramName_".$intLang)."</a>&nbsp;&nbsp;-&nbsp;&nbsp;";

					} else {
						$strTmpCon = "<a href=\"javascript:void(0)\" onMouseOver=\"HM_f_PopUp('elMenu".$kk."',event, 'top');\" onMouseOut=\"HM_f_PopDown('elMenu".$kk."')\" class=\"top_admin\" target=\"bottom\">".PMA_mysql_result($rsPg, $i, "ProgramName_".$intLang)."</a>&nbsp;&nbsp;-&nbsp;&nbsp;";
						//$strTmpCon = "<a href=\"javascript:void(0)\" onClick=\"hideAll();popUp('elMenu".$kk."',event);return false\" class=\"top_admin\">".PMA_mysql_result($rsPg, $i, "ProgramName_".$intLang)."</a>&nbsp;&nbsp;-&nbsp;&nbsp;";
						//產生下拉清單所需資料
						$intID = PMA_mysql_result($rsPg, $i, "ID");
						$strTmp = "HM_Array".$kk;
						$strSubMenu .= getSubMenu($strTmp, $intID, $strPID, $intLang);
						$kk++;

					}
					$strCon .= $strTmpCon;
					//代理商，沒有即時注單的版本，ps. 即時注單的序號是 3
					//if($objUG -> ID == 2 && PMA_mysql_result($rsPg, $i, "ID") != 3)$strConNoBetList .= $strTmpCon;
				} //for ($i = 0; $i < $intPgRows; $i++) {

				//寫入檔案
				$strFilePath = __Common_Path."/navbar/NavBar".$objUG -> ID."_".$intLang.".php";
				$strCon = "<"."?php \n\$strNavbar = \"".str_replace("\"", "\\\"", $strCon)."\";\n"."?".">";
				if(!writeFile($strFilePath, $strCon))return false;

				//寫入檔案
				$strFilePath = __Common_Path."/navbar/SubMenu".$objUG -> ID."_".$intLang.".php";
				$strCon = "<"."?php \n\$strSubMenu = \"".addslashes($strSubMenu)."\";\n"."?".">";
				if(!writeFile($strFilePath, $strCon))return false;

			} //foreach($arrLan as $strLCode){
		}

		@mysql_free_result($rsPg);
		@mysql_free_result($rsUG);
		return true;
	}  //  function getAdminMenu()  //


/**
 * 產生 Menu 檔案所需文字，ps. 管理介面 NavBar 圖層式下拉清單用
 * @param	strThisName arMenuXX 名稱
 * @param	intParentID program 父層 ID
 * @param	strPgID program id ，ex: 2,4,5,6,.......,n
 * @param	intLang 語言代碼
 * @return	成功回傳 字串
 */
	function getSubMenu($strThisName, $intParentID, $strPgID, $intLang) {
		global $_Conn, $$_Conn;
		//$tdwidth = 80;
		$intMenuTop = 70;
		$strThisMenu = "";
		$strSubMenus = "";
		$strSQL = "select *, `ProgramName_".$intLang."` as PgName from `bet_Program` where ID in (".$strPgID.") and ParentID = " .$intParentID. " order by SerialNO desc, ID";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Get Program Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$i = 0;
		$intNum = PMA_mysql_num_rows($RS);
		if ($intNum == 0)return $strThisMenu;

		if (strpos($strThisName,"y_")>0) {
			//$strThisMenu = $strThisName . " = new Array(\n";
			$strThisMenu = $strThisName . " = [\n";
		} else {
			/*$strThisMenu = $strThisName . " = new Array(\n";
			$strThisMenu .= "\"\",\n";
			$strThisMenu .= "\"20\",\"".$intMenuTop."\",\n";
			$strThisMenu .= "\"black\",\"white\",\n";
			$strThisMenu .= "\"#c0c0c0\",\"#000080\",\n";
			$strThisMenu .= "\"black\",\"#eeeeee\"";*/
			$strThisMenu = $strThisName . " = [\n";
			$strThisMenu .= "arrSetting";
		}

		$k = 1;
		while ($i < $intNum) {
			$strThisSubMenu = getSubMenu($strThisName."_".($i + 1), PMA_mysql_result($RS, $i, "ID"), $strPgID, $intLang);
			$strSubMenus .= $strThisSubMenu;

			if (strlen($strThisSubMenu) > 0) {
				if (strpos($strThisName, "y_") == 0 || $i > 0) {
					$strThisMenu .= ",\n";
				}
				//$strThisMenu .= "\"". PMA_mysql_result($RS, $i, "PgName"). "\",\"\",1";
				$strThisMenu .= "[\"". PMA_mysql_result($RS, $i, "PgName"). "\",\"\",1,0,1]";
				$k++;

			} else {
				if (strpos($strThisName, "y_") == 0 || $i > 0) {
					$strThisMenu .= ",\n";
				}
				//$strThisMenu .= "\"". PMA_mysql_result($RS, $i, "PgName"). "\",\"" . mysql_result($RS, $i, "ProgramURL")."\",0";
				$strThisMenu .= "[\"". PMA_mysql_result($RS, $i, "PgName"). "\",\"" . mysql_result($RS, $i, "ProgramURL")."\",1,0,0]";
			}
			$i++;
		}
		//$strThisMenu .= ");\n\n";
		$strThisMenu .= "];\n\n";
		$strThisMenu .= $strSubMenus;

		@mysql_free_result($RS);
		return $strThisMenu;
	}
//-------------------------------------------------------//
//AdminProgram_lib End
}
?>